import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { makeupCouponStatusArrs } from '../recyc-market-manage.pipe';
import { LoveMakeupCouponService } from '../services/love-makeup-coupon.service';

@Component({
  selector: 'app-love-makeup-coupon',
  templateUrl: './love-makeup-coupon.component.html',
  styleUrls: ['./love-makeup-coupon.component.scss']
})
export class LoveMakeupCouponComponent implements OnInit {
  // 路由缓存恢复时
  static updateCache: any = null;

  // search status 1：审核通过[已发布] 2：进行中[已发布] 3：已结束 4：已停止 5：草稿
  statusOptions = [
    {value: 1, label: '已发布'},
    {value: 3, label: '已结束'},
    {value: 4, label: '已停止'},
    {value: 5, label: '草稿'}
  ];
  // 加价券类型
  couponTypeOptions = [
    { value: 1, label: '满加券' },
    { value: 2, label: '百分比券' }
  ];
  // 适用用户
  userTypeOptions = [
    { value: 0, label: '通用' },
    { value: 1, label: 'C端' },
    { value: 2, label: 'B端' }
  ];
  // 适用成色
  userConditionOptions = [
    { value: 1, label: '二手机' },
    { value: 2, label: '准新机' }
  ];
  // 前端展示
  exhibitionOptions = [
    {value: 0, label: '不展示'},
    {value: 1, label: '展示'}
  ];
  searchOptions = [
    { type: 1, labelText: '加价券名称', valueKey: 'name', value: '' },
    { type: 1, labelText: '备注', valueKey: 'remark', value: '' },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'statusStr',
      value: [],
      multiple: true,
      notTostr: false,
      selectOptions: [...this.statusOptions],
    },
    {
      type: 2,
      labelText: '加价券类型',
      valueKey: 'type',
      value: '',
      multiple: false,
      notTostr: false,
      selectOptions: [...this.couponTypeOptions],
    },
    {
      type: 2,
      labelText: '前端展示',
      valueKey: 'isShowWeb',
      value: '',
      multiple: false,
      notTostr: false,
      selectOptions: [...this.exhibitionOptions],
    },
    {
      type: 2,
      labelText: '适用用户',
      valueKey: 'userType',
      value: '',
      multiple: false,
      notTostr: false,
      selectOptions: [...this.userTypeOptions],
    },
    {
      type: 2,
      labelText: '适用成色',
      valueKey: 'applyColour',
      value: '',
      multiple: false,
      notTostr: false,
      selectOptions: [...this.userConditionOptions],
    },
    { type: 3, labelText: '发布时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true },
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  // 手动发券
  releaseVisible = false;
  releaseLoading = false;
  opreaItem: any = {};

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private router: Router,
    private services: LoveMakeupCouponService
  ) { }

  ngOnInit(): void {
    LoveMakeupCouponComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      pageNum: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getMakeupCouponList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const { records, total } = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 下架
  async shelfData(item: any) {
    const {code, message}: any = await this.services.stopCouponItem(item.id);
    if (code === 0) {
      this.message.success('下架成功');
      this.loadTable();
    } else {
      this.message.error(message);
    }
  }

  // 删除
  async deleteData(item: any) {
    const {code, message}: any = await this.services.deleteCouponItem(item.id);
    if (code === 0) {
      this.message.success('删除成功');
      this.loadTable();
    } else {
      this.message.error(message);
    }
  }

  /**
   * 页面跳转
   * @param type 0：新增 1：编辑 2：查看 3：复制
   * @param item 
   */
  jumpPage(type: number, item?: any) {
    let params: any = {
      type: type
    }
    let url = type === 2 ? '/aisi-recyc/loveMakeupCouponDetails' : '/aisi-recyc/loveMakeupCouponEdit';

    if (item) {
      params.id = item.id;
    }
    this.router.navigate([url], {
      queryParams: params
    })
  }

  /**
   * 手动发券
   */
  showCouponModal(item: any) {
    this.opreaItem = item;
    this.releaseVisible = true;
  }

  async submitManual(val: any) {
    this.loadTable();
  }

  modelToText(str: string) {
    if (str == '0') {
      return '全部';
    } else if (str) {
      return str.split(',').length;
    } else {
      return '-';
    }
  }
}
